﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Animation;
using System.Windows.Markup;
using System.Windows.Controls.Primitives;
using System.Collections.ObjectModel;

namespace UmlControlLibrary
{
    public enum UmlRelationType { Inheritance, Association, Composition, Aggregate, Note };
    

    /// <summary>
    /// - An UmlRelation describes the relation between 2 UmlElements:
    /// - End1 is bound to an UmlProperty (except for Inheritance), while End2 is bound to a class, an interface, or an enum.
    /// - A relation should be created when a property is added, and the other end,
    /// if non existent, should be created if it doesn't exist.
    /// - A relation should be destroyed when one end (ie the property, or the class/enum)
    /// is removed.
    /// - A relation possesses a name, equal to end1 name + end2 name, modifiable
    /// - A relation possesses a type, describing the relation from end1 to end2
    /// - Each end possesses a multiplicity, a modifier, and a IsNavigable attribute.
    /// - End1's name is equal to end2 element name, and modifiable
    /// - End2's name is equal to end1 property name, and modifiable as well, except that name
    /// modification means UmlProperty name modification
    /// - At last, a relation is responsible for adding itself to UmlElementList
    /// - A Class, Interface, Enum, or anything else is not aware of relations.
    /// (However, as a relation is a UmlElement, the GetChilds method of an element can
    /// ask for relations to the Diagram - After all, extremism is *bad*)
    /// Instead, the diagram is aware of relations, and responsible for creation, modification, 
    /// and deletion of relations.
    /// </summary>
    public class UmlRelation : UmlElement
    {
        public class Events
        {
            public static Event Created = new Event(Event.Created, typeof(UmlRelation));
            public static Event Modified = new Event(Event.Modified, typeof(UmlRelation));
            public static Event Deleted = new Event(Event.Deleted, typeof(UmlRelation));
        }

        private UmlRelationEnd _end1;
        public UmlRelationEnd End1
        {
            get { return _end1; }
            set
            {
                _end1 = value;
                OnPropertyChanged("End1");
            }
        }

        private UmlRelationEnd _end2;
        public UmlRelationEnd End2
        {
            get { return _end2; }
            set
            {
                _end2 = value;
                OnPropertyChanged("End2");
            }
        }

        private UmlRelationType _relationType;
        public UmlRelationType RelationType
        {
            get { return _relationType; }
            set
            {
                _relationType = value;
                OnPropertyChanged("RelationType");
            }
        }

        public UmlRelation(UmlElement element1, UmlElement element2, UmlRelationType relationType)
        {
            Name = element1.Name + element2.Name;
            End1 = new UmlRelationEnd(element1);
            End2 = new UmlRelationEnd(element2);
            End1.Label = element2.Name;
            End1.Label = element1.Name;
            //UmlModel.CurrentDiagram.AddElement(this);
            RelationType = relationType;
            EventService.BroadcastEvent(this, UmlRelation.Events.Created);
        }

        public override ObservableCollection<UmlElement> GetChilds()
        {
            ObservableCollection<UmlElement> elements = new ObservableCollection<UmlElement>();
            elements.Add(End1);
            elements.Add(End2);
            return elements;
        }
    }


}
